// 昵称
var $nameinput = $('#nickname');
var $name = $nameinput.parent('.item-name');
var $err = $name.children('.tip-err').children('.tip-txt');
var $next = $('.slide-next');

function checkName() {
	var val = $.trim($nameinput.val());
	var valid;

	if (!val) {
		valid = false;
		$name.addClass('error');
		$err.text('请填写昵称');
	} else {
		$.post(U('home/Public/isValidNickName'), {nickname: val})
		.done(function(date) {
			if (date === 'success') {
				$name.removeClass('error').addClass('valid');
			} else {
				valid = false;
				$err.text(date);
				$name.removeClass('valid').addClass('error');
			}
		});
	}

	return valid;
}

$nameinput.blur(function() {
	checkName();
}).focus(function() {
	$name.removeClass('error valid');
});

$('#J_startForm').submit(function(e) {
	if ($name.hasClass('error') || checkName() === false) return false;

	// ajax 提交表单
	e.preventDefault();
	var $this = $(this);
	$.post(this.action, $this.serialize())
	.done(function(data) {
		if (data == 1) {
			location.href = $next[0].href;
		} else if (data == 2) {
			$err.text('昵称已存在');
			$name.removeClass('valid').addClass('error');
		} else if (data == 0) {
			alert('资料保存失败，请检查您的输入！');
		}
	});
});

// 下一步
$next.click(function(e) {
	if ($name.hasClass('error')) {
		e.preventDefault();
	} else {
		$('#J_startForm').submit();
	}
});

// 地区
function initProvinceCity(province, provinceOption, city, cityOption) {
	var p = $('#' + province),
		po = $('#' + provinceOption),
		pv = p.children('.select-input'),
		pi = p.children('input'),
		c = $('#' + city),
		co = $('#' + cityOption),
		cv = c.children('.select-input'),
		ci = c.children('input');

	$(document).on('click', function(e){
		po.hide();
		co.hide();
	});

	$.post(U('home/Public/getProvince'), {id:0}, function(data) {
		var dataObj=eval("("+data+")");
		$.each(dataObj, function(index, val){
			html += '<a href="javascript:;" data-index="' + val['area_id'] + '">' + val['title'] + "</a>";
		});
		po.html(html).show();
	});

	p.on('click', function(e){
		co.hide();
		cv.text('城市');//选择省份时，重置城市
		var html = '';
		$.post(U('home/Public/getProvince'), {id:0}, function(data) {
			var dataObj=eval("("+data+")");
			$.each(dataObj, function(index, val){
				html += '<a href="javascript:;" data-index="' + val['area_id'] + '">' + val['title'] + "</a>";
			});
			po.html(html).show();
		});
		e.stopPropagation();
	});

	po.on('click', 'a', function(){
		var i = $(this).data('index');
		var html = '';
		pv.attr('data-index',i);
		pv.text($(this).text());
		pi.val(i);
		po.hide();
		$.post(U('home/Public/getProvince'),{id:i},function(data) {
			var dataObj = eval("("+data+")");
			$.each(dataObj,function(index,val) {
				html += '<a href="javascript:;" id="cityArea_id" data-index="' + val['area_id'] + '">' +	val['title'] + "</a>";
			});
			co.html(html).show();
		});
		return false;
	});

	c.on('click', function(e){
		po.hide(); //防止出现省份与城市列表一块出现
		co.show();
		e.stopPropagation();
	});

	co.on('click', 'a', function(){
		var i = $(this).data('index');
		cv.attr('data-index', i);
		cv.text($(this).text());
		ci.val(i);
		co.hide();
		return false;
	});
}

initProvinceCity("province", "province-option", "city", "city-option");
